package com.zw.admin.server.dao;

import com.zw.admin.server.commom.TkMapper;
import com.zw.admin.server.model.Space;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface SpaceDao extends TkMapper<Space> {

    @Select("select bsn_space.* FROM bsn_space " +
            "LEFT JOIN user_space on bsn_space.space_id = user_space.space_id " +
            "where user_space.user_id = ${params.userId}  " +
            "limit #{offset}, #{limit}")
    List<Space> listAll(@Param("params")Map<String, Object> params, @Param("offset")Integer offset, @Param("limit")Integer limit);

    @Select("select bsn_space.* FROM bsn_space " +
            "LEFT JOIN user_space on bsn_space.space_id = user_space.space_id " +
            "where user_space.user_id = ${params.userId} AND bsn_space.space_name like '%${params.spaceName}%' " +
            "limit #{offset}, #{limit}")
    List<Space> listByName(@Param("params")Map<String, Object> params, @Param("offset")Integer offset, @Param("limit")Integer limit);

    @Select("select count(1) FROM bsn_space " +
            "LEFT JOIN user_space on bsn_space.space_id = user_space.space_id " +
            "where user_space.user_id = ${params.userId}  AND bsn_space.space_name like '%${params.spaceName}%'" )
    String countSpace(@Param("params")Map<String, Object> params);

    @Delete("delete from bsn_space where space_id = #{space_id}")
    int deleteSpace(Long space_id);

    @Select("select * FROM bsn_space where parent_id = 0" )
    List<Space> getTopNode();

    @Select("select * FROM bsn_space where parent_id = #{spaceId}" )
    List<Space> getSubNode(@Param("spaceId")String spaceId);

    @Select({
            "<script>",
            "select",
            "device_id",
            "from bsn_space_device",
            "where space_id in",
            "<foreach collection='spaceIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<String> getSpaceDevice(@Param("spaceIds")ArrayList<String> spaceIds);

    @Select("select * FROM bsn_space where 1=1" )
    List<Space> getSpaceList();
}
